package com.programmercarl.backtracking;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.cn/problems/permutations/
 * <p>
 * 全排列
 *
 * @author yulongtian
 * @create 2025-01-12 12:59
 */
public class Test11 {
    public static void main(String[] args) {
        System.out.println(new Test11().permute(new int[]{1, 2, 3}));
    }

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        boolean[] used = new boolean[nums.length];
        backTrack(nums, res, path, used);
        return res;
    }

    public void backTrack(int[] nums, List<List<Integer>> ans, List<Integer> path, boolean[] used) {
        if (path.size() == nums.length) {
            ans.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (used[i]) {
                continue;
            }
            used[i] = true;
            path.add(nums[i]);
            backTrack(nums, ans, path, used);
            used[i] = false;
            path.remove(path.size() - 1);
        }
    }
}
